﻿Public Class UC_QuanLyNhanVien

    Private Sub PanelControl1_Paint(sender As Object, e As PaintEventArgs) Handles PanelControl1.Paint

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim f As New F_NhanVien
        f.ShowDialog()
        loadGvNhanvien
    End Sub

    Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
        sendDataToFormNhanVien()
        loadGvNhanvien
    End Sub
    Dim nv As New DTONhanVien
    Dim tk As New DTOTaiKhoan
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If AgreeForm.deleteCaution = False Then
            Return
        End If
        nv.MaNV = Convert.ToInt16(gvNhanvien.CurrentRow.Cells("MaNV").Value)
        BUSNHANVIEN.NhanVien_Delete(nv)
        tk.MaNV = nv.MaNV
        BUSTaiKhoan.TaiKhoan_Delete(tk)
        loadGvNhanvien
    End Sub
    Sub getDataGv()
        nv.MaNV = Convert.ToInt16(gvNhanvien.CurrentRow.Cells("MaNV").Value)
        nv.TenNV = gvNhanvien.CurrentRow.Cells("tennv").Value
        nv.GioiTinh = gvNhanvien.CurrentRow.Cells("Gioitinh").Value
        nv.NgaySinh = Convert.ToDateTime(gvNhanvien.CurrentRow.Cells("ngaysinh").Value)
        nv.MaCV = gvNhanvien.CurrentRow.Cells("Macv").Value
        nv.DiaChi = gvNhanvien.CurrentRow.Cells("diachi").Value
        nv.DienThoai = gvNhanvien.CurrentRow.Cells("Dienthoai").Value
    End Sub
    Public Sub sendDataToFormNhanVien()
        getDataGv()
        Dim f As New F_NhanVien
        f.recieveData(nv, 0) ' 0 co nghia la hanh dong update
        f.ShowDialog
    End Sub
    Public Sub loadGvNhanvien()
        gvNhanvien.DataSource = BUSNHANVIEN.NhanVien_Load
    End Sub
    Private Sub UC_QuanLyNhanVien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadGvNhanvien
    End Sub

    Private Sub SimpleButton4_Click(sender As Object, e As EventArgs)
        loadGvNhanvien()
    End Sub

    Private Sub btnQLTK_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnThemtk_Click(sender As Object, e As EventArgs) Handles btnThemtk.Click
        If txtTenTK.Text = "" Then
            MsgBox("Tên tài khoản không được để trống")
            Return
        End If
        Dim t As New DTOTaiKhoan
        t.Password = "123"
        t.Username = txtTenTK.Text
        t.MaNV = nv.MaNV
        BUSTaiKhoan.TaiKhoan_Insert(t)
    End Sub

    Private Sub gvNhanvien_SelectionChanged(sender As Object, e As EventArgs) Handles gvNhanvien.SelectionChanged
        getDataGv()
        Dim sql As String = "select * from taikhoan where Manv = " & nv.MaNV
        Dim dt As New DataTable
        dt = DataProvider.LoadTable(sql)
        If dt.Rows.Count <> 0 Then
            ' MsgBox("Nhân viên đã có tài khoản")
            'Return
            btnThemtk.Enabled = False
        Else
            btnThemtk.Enabled =True
        End If
    End Sub
End Class
